package com.yc.cloud.portal.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.basic.config.PathUtils;
import com.yc.cloud.basic.dto.BaseQueryParam;
import com.yc.cloud.basic.utils.SnowflakeIdGenerator;
import com.yc.cloud.entity.Question;
import com.yc.cloud.mapper.QuestionMapper;
import com.yc.cloud.portal.dto.request.QuestionCreateParam;
import com.yc.cloud.portal.service.QuestionService;
import com.yc.cloud.security.utils.MemberUtils;
import lombok.SneakyThrows;
import lombok.val;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 企业 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2024-01-31
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    private SnowflakeIdGenerator snowflakeIdGenerator;

    @PostConstruct
    public void init() {
        snowflakeIdGenerator = new SnowflakeIdGenerator(1L);
    }

    @Override
    public IPage<Question> list(BaseQueryParam queryParam) {
        val query = new QueryWrapper<Question>();
        IPage<Question> page;
        //排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(queryParam.getSortBy());
            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(queryParam.getSortBy());
            }
        }
        //模糊查询
        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
            query.and(queryWrapper -> {

            });
        }
        //分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        return baseMapper.selectPage(page, query);
    }

    @Override
    public Question create(QuestionCreateParam param) {
        val question = new Question();
        BeanUtils.copyProperties(param, question);
        question.setQuestionNo(generateQuestionNo());
        question.setCompanyId(MemberUtils.getCurrentCompanyId());
        val saveFlag = save(question);
        return saveFlag ? question : null;
    }

    @Override
    public Question get(Long id) {
        return getById(id);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }


    @Override
    public Question update(Long id, Question param) {
        val question = new Question();
        BeanUtils.copyProperties(param, question);
        question.setId(id);
        val updateFlag = updateById(question);
        return updateFlag ? question : null;
    }


    @Override
    public Question convertVo(Question question) {
        if (question == null) {
            return null;
        }
        val vo = new Question();
        BeanUtils.copyProperties(question, vo);
        return vo;
    }

    @Override
    public List<Question> convertVo(List<Question> QuestionList) {
        if (CollUtil.isNotEmpty(QuestionList)) {
            return QuestionList.stream().map(this::convertVo).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    private String generateQuestionNo() {
        return String.valueOf(snowflakeIdGenerator.generateId());
    }
}
